package com.example.zhangxin.comevedio.activity.utils;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Xml;

import com.example.zhangxin.comevedio.activity.bean.Version;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * Created by ZhangXin on 2016/9/13.
 */

public class VersionUtil {
    public static Version getVersion(String xml){
        Version v = new Version();
        try {
            byte[] bytes = xml.getBytes();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(stream,"utf-8");
            int type = parser.getEventType();
            while (type!=XmlPullParser.START_DOCUMENT){
                switch (type){
                    case XmlPullParser.START_TAG:
                        if("version".equals(parser.getName())){
                            v.setVersion(parser.nextText());
                        }else if ("info".equals(parser.getName())) {
                            v.setInfo(parser.nextText());
                        }else if ("url".equals(parser.getName())) {
                            v.setUrl(parser.nextText());
                        }
                        break;
                }
                type = parser.next();
            }
            return v;
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void installApk(File file, Context context){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
        context.startActivity(intent);
    }
}
